package com.shuyu.web.controller.platform;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuyu.common.core.controller.BaseController;
import com.shuyu.common.core.domain.AjaxResult;
import com.shuyu.common.core.page.TableDataInfo;
import com.shuyu.common.utils.SecurityUtils;
import com.shuyu.domain.Message;
import com.shuyu.domain.MessageHistory;
import com.shuyu.service.MessageHistoryService;
import com.shuyu.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 授权登录相关
 * 
 * @author
 */
@RestController
@RequestMapping("/platform/message")
@Api(tags = "用户留言")
public class PMessageController extends BaseController
{
    @Resource
    private MessageService messageService;
    @Resource
    private MessageHistoryService messageHistoryService;



    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/list")
    @ApiOperation("用户留言列表")
    public TableDataInfo list()
    {
        startPage();
        List<Message> list = messageService.getList();
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:message:update')")
    @PutMapping
    @ApiOperation("回复用户留言信息")
    public AjaxResult update(@RequestBody MessageHistory messageHistory){
        Long userId = SecurityUtils.getLoginUser().getUserId();
        messageHistory.setMessageType(1);
        messageHistory.setCreateBy(userId);
        messageHistory.setCreateTime(new Date());
        messageHistoryService.save(messageHistory);
        Message message = messageService.getById(messageHistory.getMessageId());
        message.setId(messageHistory.getMessageId());
        message.setStatus(1);
        message.setReplyTime(new Date());
        message.setReply(messageHistory.getMessage());
        message.setReadStatus(0);
        return toAjax(messageService.updateById(message));
    }

    @GetMapping("/{messageId}")
    @ApiOperation("获取用户某条留言详情")
    public TableDataInfo detail(@PathVariable int messageId){
        List<MessageHistory> list = messageHistoryService.list(new LambdaQueryWrapper<MessageHistory>().eq(MessageHistory::getMessageId, messageId)
                .orderByAsc(MessageHistory::getCreateTime));
        return getDataTable(list);
    }

}
